package generador;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Vector;

public class VectorCaracteres extends Vector<Character> {

	private static final long serialVersionUID = 1L;
	private BufferedReader bufferedReader;
	private boolean hayMasCaracteres = true;

	public VectorCaracteres(BufferedReader bufferedReader) {
		this.bufferedReader = bufferedReader;
		this.llenarVector();
	}

	// devuelve objetos Character. Si es el fin de archivo, devuelve el $
	private Character getCharFromBuffer() {
		try {
			int i;
			i = this.bufferedReader.read();
			if (i != -1) {
				return new Character((char) i);
			} else {
				return new Character('$');
			}
		} catch (IOException e) {
			return null;
		}
	}

	private void llenarVector() {
		this.clear();
		boolean seguir = true;
		int i = 0;
		Character temp = null;
		while (seguir && i < TiposConstantes.MAX_BUFFER) {
			temp = this.getCharFromBuffer();
			this.add(temp);
			i++;
			if (temp.equals('$')) {
				seguir = false;
				this.hayMasCaracteres = false;
			}

		}

	}

	public Character getChar() {
		if (this.size() == 0) {
			if (hayMasCaracteres) {
				this.llenarVector();
				return getChar();
			} else {
				return new Character('$');
			}
		} else {
			return remove(0);
		}
	}

}
